package com.rpay.dao.user;


import com.rpay.dao.mapper.UserTradeDOMapper;
import com.rpay.model.user.UserTradeDO;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Title：
 * Description
 */
@Repository
public class UserTradeDao {

    @Autowired
    @Qualifier("readSqlSession")
    private SqlSessionTemplate readSqlSession;
    @Autowired
    private UserTradeDOMapper userTradeDOMapper;
    @Autowired
    @Qualifier("writeSqlSession")
    private SqlSessionTemplate writeSqlSession;

    public List<Map> selectUserTradeList(Map map, RowBounds rowBounds) {
//        readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectUserTradeList", map);
        List list = readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectUserTradeList", map, rowBounds);
//        Page page = (Page) list;
        return list;
    }


    public List<Map> selectUserTradeLists(Map map, RowBounds rowBounds) {

//        return readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectUserTradeList", map, rowBounds);
        return readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectUserTradeList", map);
    }

    public int updateUserTradeStatus(UserTradeDO userTradeDO) {
        return userTradeDOMapper.updateByPrimaryKeySelective(userTradeDO);
//        return readSqlSession.update("com.rpay.dao.mapper.UserTradeDOMapper.updateByPrimaryKeySelective", userTradeDO);

    }

    public List<Map> selectSelfTradeList(Map map, RowBounds rowBounds) {
        List list = readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectSelfTradeList", map, rowBounds);
//        Page page = (Page) list;
        return list;
    }

    public List<UserTradeDO> selectUserTradesByExecTime() {
        return readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectUserTradesByExecTime");
    }

    public int insert(UserTradeDO userTradeDO) {
        return userTradeDOMapper.insertSelective(userTradeDO);
//        return userTradeDOMapper.insert(userTradeDO);
    }

    public List<UserTradeDO> getPendingAuditTrade() {
        return readSqlSession.selectList("com.rpay.dao.mapper.UserTradeDOMapper.selectPendingAudit");
    }

    public UserTradeDO findById(int tradeId) {
        return readSqlSession.selectOne("com.rpay.dao.mapper.UserTradeDOMapper.findById", tradeId);
    }

    public int patch(UserTradeDO userTradeDO) {
        return writeSqlSession.update("com.rpay.dao.mapper.UserTradeDOMapper.patch", userTradeDO);
    }

    /**
     * 获取用户交易的数量
     *
     * @param param ：{type,status,userId,startDate,endDate}
     * @return
     */
    public int getUserTradeCount(Map param) {
        return userTradeDOMapper.selectUserTradeCount(param);
    }

}
